package com.shuyu.web.controller.platform;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuyu.common.core.controller.BaseController;
import com.shuyu.common.core.domain.AjaxResult;
import com.shuyu.common.core.domain.Result;
import com.shuyu.common.core.domain.entity.Customer;
import com.shuyu.common.core.page.TableDataInfo;
import com.shuyu.common.utils.DesCode;
import com.shuyu.common.utils.StringUtils;
import com.shuyu.common.utils.poi.ExcelUtil;
import com.shuyu.domain.CustomerBehavior;
import com.shuyu.service.CustomerAssetsService;
import com.shuyu.service.CustomerBehaviorService;
import com.shuyu.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 授权登录相关
 * 
 * @author
 */
@RestController
@RequestMapping("/platform/customer")
@Api(tags = "用户管理")
public class PCustomerController extends BaseController
{
    @Resource
    private CustomerService customerService;
    @Resource
    private CustomerAssetsService customerAssetsService;
    @Resource
    private CustomerBehaviorService customerBehaviorService;

    @PreAuthorize("@ss.hasPermi('system:customer:list')")
    @GetMapping("/list")
    @ApiOperation("查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sex", value = "性别 0未知 1男 2女", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "source", value = "注册来源(0:微信  1:大友)", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "province", value = "省", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "city", value = "市 ", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "districts", value = "区 ", dataType = "String", dataTypeClass = String.class),

    })
    public TableDataInfo list(Integer sex,Integer source,String province,String city,String districts)
    {
        startPage();
        List<Customer> list = customerService.list(new LambdaQueryWrapper<Customer>()
                .eq(Customer::getDelFlag,0)
                .eq(ObjectUtil.isNotNull(sex), Customer::getSex,sex)
                .eq(ObjectUtil.isNotNull(source), Customer::getSource,source)
                .eq(StringUtils.isNotEmpty(province),Customer::getProvince, DesCode.encrypt(province))
                .eq(StringUtils.isNotEmpty(city),Customer::getCity,DesCode.encrypt(city))
                .eq(StringUtils.isNotEmpty(districts),Customer::getDistricts,DesCode.encrypt(districts))
                .orderByDesc(Customer::getCreateTime));
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:customer:assets')")
    @GetMapping("/assets")
    @ApiOperation("查询用户资产列表")
    @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", dataTypeClass = String.class)
    public TableDataInfo<List<Customer>> assets(String username)
    {
        startPage();
        List<Customer> list = customerAssetsService.getCustomerAssets(username);
        return getDataTable(list);
    }

    @GetMapping("/{customerId}")
    @ApiOperation("查询用户详情")
    @ApiImplicitParam(name = "customerId", value = "用户ID", dataType = "Integer", dataTypeClass = Integer.class)
    public Result<Customer> detail(@PathVariable long customerId)
    {
        Customer detail = customerService.getById(customerId);
        return Result.success(detail);
    }

    @PreAuthorize("@ss.hasPermi('system:customer:behavior')")
    @GetMapping("/behavior")
    @ApiOperation("查询用户行为列表")
    public TableDataInfo behavior()
    {
        startPage();
        List<CustomerBehavior> list = customerBehaviorService.list(new LambdaQueryWrapper<CustomerBehavior>()
                .orderByDesc(CustomerBehavior::getCreateTime));
        return getDataTable(list);
    }
    @PostMapping("/behavior/export")
    @ApiOperation("查询用户行为列表导出")
    public void behaviorExport(HttpServletResponse response)
    {
        List<CustomerBehavior> list = customerBehaviorService.list(new LambdaQueryWrapper<CustomerBehavior>()
                .orderByDesc(CustomerBehavior::getCreateTime));
        for (CustomerBehavior customerBehavior : list) {
            customerBehavior.setLevelOne(customerBehavior.getBehavior());
        }
        ExcelUtil<CustomerBehavior> util = new ExcelUtil<>(CustomerBehavior.class);
        util.exportExcel(response, list, "用户行为数据");
    }

    @PreAuthorize("@ss.hasPermi('system:customer:behaviorTimeLine')")
    @GetMapping("/behaviorTimeLine")
    @ApiOperation("查询用户行为时间线")
    public AjaxResult behaviorTimeLine(long eventId)
    {
        List<CustomerBehavior> list = customerBehaviorService.list(new LambdaQueryWrapper<CustomerBehavior>().eq(CustomerBehavior::getEventId,eventId)
                .orderByDesc(CustomerBehavior::getCreateTime));
        return AjaxResult.success(list);
    }
}
